package edu.xlh.travel.controller;

import com.baidu.aip.ocr.AipOcr;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wf.captcha.SpecCaptcha;
import edu.xlh.travel.common.Result;
import edu.xlh.travel.service.CaptchaService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/captcha")
public class CaptchaController {
    @Autowired
    private CaptchaService captchaService;


    @ResponseBody
    @RequestMapping("/getArithmeticCaptcha")
    public Result getArithmeticCaptcha() throws Exception {
        return captchaService.getArithmeticCaptcha();
    }

    @ResponseBody
    @RequestMapping("/getChineseGifCaptcha")
    public Result getChineseGifCaptcha() throws Exception {
        return captchaService.getChineseGifCaptcha();
    }
   /* @ResponseBody
    @PostMapping("/login")
    public Result login(String username,String password,String verCode,String verKey){
        // 获取redis中的验证码
        String redisCode = redisUtil.get(verKey);
        // 判断验证码
        if (verCode==null || !redisCode.equals(verCode.trim().toLowerCase())) {
            retur new Result();
        }
    }*/
   @ResponseBody
   @RequestMapping("/fillVercodeByButton")
    public Result fillVercodeByButton(@RequestBody String base64) throws IOException {
        base64 = base64.substring(23);
       base64 = base64.replaceAll("\r\n", "");
       base64 = base64.replaceAll("\\+", "%2B");
       System.out.println(base64);
       AipOcr client = new AipOcr("25525109", "Xasvah511T2Obs6FpPkD8U3D", "zR1Am5LxL99YHPs1rfIN15ttvfD1Ribk");
       // 传入可选参数调用接口
       HashMap<String, String> options = new HashMap<String, String>(4);
       options.put("language_type", "CHN_ENG");
       options.put("detect_direction", "true");
       options.put("detect_language", "true");
       options.put("probability", "true");
       // 参数为二进制数组
           byte[] buf = base64.getBytes();
       JSONObject res = client.basicGeneral(buf, options);
       ObjectMapper mapper = new ObjectMapper();
       mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
       System.out.println(mapper.readValue(res.toString(), Map.class));
       return new Result(200 , "识别成功" , mapper.readValue(res.toString(), Map.class));

       /*return captchaService.fillVercodeByButton(base64);*/
    }
}
